//
//  GongLvXiangQingViewController.m
//  礼物说
//
//  Created by dllo on 16/4/27.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "GongLvXiangQingViewController.h"
#import "AppTools.h"

#define kGONGLVXIANGQINGURL [NSString stringWithFormat:@"http://api.liwushuo.com/v2/posts/%@", num]

@interface GongLvXiangQingViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate, UIWebViewDelegate, UMSocialUIDelegate>

@property (nonatomic, retain) GongLvXiangQingModel *model;
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) BaseCollectionView *collectionView;
@property (nonatomic, retain) BaseTableView *tableView;
@property (nonatomic, assign) NSInteger count;
/** 图片数组*/
@property (nonatomic, retain) NSArray *array;

@end

@implementation GongLvXiangQingViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc {
    [_number release];
    [_gongLvNumber release];
    [_judge release];
    [_tableView release];
    [_collectionView release];
    [_webView release];
    [_array release];
    [_model release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"攻略详情";
    self.tabBarController.tabBar.translucent = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    [GiFHUD show];
    
#pragma maek - 创建BarButton
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"嗨购" style: UIBarButtonItemStylePlain target:self action:@selector(buttonAction)];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = YES;
    /** 自动*/
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    if ([_judge isEqualToString:@"收藏"]) {
        [self dataHandle:_gongLvNumber];
    }else {
        [self dataHandle:_number];
    }
}

- (void)buttonAction{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 数据解析
- (void)dataHandle:(NSNumber * )num {
    
    [XRAFNetworkTool GETUrl:kGONGLVXIANGQINGURL body:nil headFile:nil response:XRJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *dic = responseObject[@"data"];
        self.model = [GongLvXiangQingModel modelWithDic:dic];
        [GiFHUD dismiss];
        
        [self createTableView];
        [self createCollection];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
        [GiFHUD dismiss];
    }];
}

#pragma mark - 创建TableView
- (void)createTableView {
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, kWIDTHS(375), kHEIGHT - 49)];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[ButtomTableViewCell class] forCellReuseIdentifier:@"tableViewCell"];
    
#pragma mark - WebView
    UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kWIDTHS(375), kHEIGHTS(667))];
    webView.delegate = self;
    [webView loadHTMLString:_model.content_html baseURL:nil];
    self.tableView.tableFooterView = webView;
    webView.scrollView.scrollEnabled = NO;
    [webView release];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    CGRect rect = webView.frame;
    rect.size.height = webView.scrollView.contentSize.height;
    webView.frame = rect;
    self.tableView.tableFooterView = webView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ButtomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
    cell.model = _model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kHEIGHTS(300);
}

#pragma mark - 创建Collection {
- (void)createCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1;
    layout.itemSize = CGSizeMake((kWIDTH - 4) / 2, 49);
    layout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, kHEIGHT - 49, kWIDTH, kHEIGHTS(49)) collectionViewLayout:layout];
    [self.view addSubview: _collectionView];
    [_collectionView release];
    [layout release];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[GongLvButtomCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GongLvButtomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        cell.number = _model.likes_count;
        cell.imgView.image = [UIImage imageNamed:@"heart_32px_1175797_easyicon.net.png"];
        return cell;
    }else if (indexPath.row == 1) {
        cell.number = _model.shares_count;
        cell.imgView.image = [UIImage imageNamed:@"fenxiang.png"];
    }
    return cell;
}

#pragma mark - 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        /** 数据库*/
        /** 查询*/
            NSArray *arr = [[DatabaseHandle shareDataBase] selectDataGongLvWithName:_model.title];
            
            if (arr.count == 0) {
                gongLv *gonglv = [[[gongLv alloc] init] autorelease];
                gonglv.name = _model.title;
                gonglv.picture = _model.cover_image_url;
                gonglv.url = (NSString *) _model.number;
                gonglv.like = (NSString *) _model.likes_count;
                
                /** 插入*/
                [[DatabaseHandle shareDataBase] insertDataWithGongLv:gonglv];
                
                [AppTools createAlert:@"提示" viewController:self str1:@"收藏成功" back:NO];
        }
        
        [AppTools createAlert:@"提示" viewController:self str1:@"已收藏" back:NO];
        
    }else if (indexPath.row == 1) {
        
//        UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
//        /** Sns平台类，用`platformName`作为标识，指定显示名称、显示的图片，点击之后的响应。*/
//        snsPlatform.displayName = @"新浪";
//        
//        UMSocialSnsPlatform *snsPlatform2 = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToTencent];
//        snsPlatform2.displayName = @"腾讯微博";
        
//        [UMSocialSnsService presentSnsIconSheetView:self appKey:@"57216d4de0f55a1a4a00412e" shareText:@"测试" shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,nil]
//                                           delegate:self];
        
//        [UMSocialSnsService presentSnsIconSheetView:self
//                                             appKey:@"57216d4de0f55a1a4a00412e"
//                                          shareText:@"你要分享的文字"
//                                         shareImage:[UIImage imageNamed:@"icon.png"]
//                                    shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQzone,UMShareToQQ,UMShareToRenren,UMShareToDouban,UMShareToEmail,UMShareToSms,UMShareToFacebook,UMShareToTwitter,nil]
//                                           delegate:self];

  }
}

#pragma mark - 弹出提示对话框
//- (void)createAlsert:(NSString *)str str1:(NSString *)str1 back:(BOOL)back{
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:str message:str1 preferredStyle: UIAlertControllerStyleAlert];
//    
//    [self presentViewController:alert animated:YES completion:nil];
//    
//    [UIView animateWithDuration:10 animations:^{
//        
//        [self dismissViewControllerAnimated:YES completion:^{
//            
//            if (back) {
//                
//                [self.navigationController popToRootViewControllerAnimated:YES];
//            }
//        }];
//    }];
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
